/*
 * @author java_deng
 * @date 2024/12/4 11:58
 * @description
 */
package org.kaka.controller;

import org.kaka.dto.LikeDto;
import org.kaka.dto.PostDto;
import org.kaka.po.PostPo;
import org.kaka.service.PostService;
import org.kaka.service.impl.PostServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/post")
public class PostController extends BaseController{
    @Autowired
    private PostServiceImpl postService;

    @PostMapping("/list")
    public Object postList(@RequestBody PostPo postPo){
        if(postPo.getType()!=null){
            if(postPo.getType()==1){
                postPo.setBookName(postPo.getSearch());
            } else if (postPo.getType() == 2) {
                postPo.setTitle(postPo.getSearch());
            }
        }
        return success(postService.list(postPo));
    }

    @PostMapping("/audit")
    public Object audit(@RequestBody PostPo postPo){
        postService.audit(postPo);
        return success("审核成功");
    }

    @PostMapping("/managerList")
    public Object list(@RequestBody PostPo postPo){
        return success(postService.managerList(postPo));
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody PostDto postDto){
        if(postDto.getId()!=null && postDto.getId()!=0){
            postService.update(postDto);
        }
        else {
            try {
                checkParams(postDto);
            } catch (Exception e) {
                return fail(e.getMessage());
            }
            postService.save(postDto);
        }
        return success("新增成功");
    }

    @GetMapping("/detail")
    public  Object detail(Long id,Long userId){
        if(id==null){
            return fail("id不能为空");
        }
        return postService.detail(id,userId);
    }

    @PostMapping("/like")
    public Object like(@RequestBody LikeDto likeDto){
        postService.like(likeDto);
        return success("点赞成功");
    }

    @PostMapping("/cancelLike")
    public Object cancelLike(@RequestBody LikeDto likeDto){
        postService.cancelLike(likeDto);
        return success("取消点赞成功");
    }

    @GetMapping("/delete")
    public Object delete(Long id){
        if(id==null){
            return fail("id不能为空");
        }
        postService.delete(id);
        return success("删除成功");
    }

    private void checkParams(PostDto postDto) throws Exception {
        String title = postDto.getTitle();
        String desc = postDto.getDesc();
        Long bookId = postDto.getBookId();
        Long userId = postDto.getUserId();
        Integer categoryId = postDto.getCategoryId();
        Long imageId = postDto.getImageId();
        if(title==null || title.isEmpty()){
            throw new Exception("title不能为空");
        }
        if(desc==null || desc.isEmpty()){
            throw new Exception("desc不能为空");
        }
        if(bookId==null || bookId==0){
            throw new Exception("bookId不能为空");
        }
        if(userId==null || userId==0){
            throw new Exception("userId不能为空");
        }
        if(categoryId==null || categoryId==0){
            throw new Exception("categoryId不能为空");
        }
        if(imageId==null || imageId==0){
            throw new Exception("imageId不能为空");
        }
    }
}
